<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%
    JSONObject jsonData = new JSONObject();
    String formid = Util.null2String(request.getParameter("formid"));
    String ids = Util.null2String(request.getParameter("ids"));
    String curdate = DateUtil.getCurrentDate();
    RecordSet rs = new RecordSet();
    try {
        ResourceComInfo comInfo = new ResourceComInfo();
        rs.executeQuery("select * from uf_zjjhhzb where id in (" + ids + ") and nvl(sfcffklc,1) != 0");
        int counts = rs.getCounts();
        int succcount = 0;
        int failcount = 0;
        String requestid = "0";
        String jbr = "";
        while (rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            jbr = Util.null2String(rs.getString("jbr"));
            String bm = Util.null2String(rs.getString("bm"));
            String jyzfnr = Util.null2String(rs.getString("jyzfnr"));
            String zffsxl = Util.null2String(rs.getString("zffsxl"));
            String cwfhjeyb = Util.null2String(rs.getString("cwfhjeyb"));
            String ksbm = Util.null2String(rs.getString("ksbm"));
            String ksmc = Util.null2String(rs.getString("ksmc"));
            String lxhdh = Util.null2String(rs.getString("lxhdh"));
            String khyxdh = Util.null2String(rs.getString("khyxdh"));
            String yxzhdh = Util.null2String(rs.getString("yxzhdh"));
            String lxhcd = Util.null2String(rs.getString("lxhcd"));
            String khyxcd = Util.null2String(rs.getString("khyxcd"));
            String yxzhcd = Util.null2String(rs.getString("yxzhcd"));

            String zjmxxmwb = Util.null2String(rs.getString("zjmxxmwb"));
            String jhid = Util.null2String(rs.getString("jhid"));
            String lzdjh = Util.null2String(rs.getString("lzdjh"));
            String bz = Util.null2String(rs.getString("bz"));
            String gsdm = Util.null2String(rs.getString("gsdm"));
            String ssgs =getGsxx(gsdm);
            String wlkmbm = Util.null2String(rs.getString("wlkmbm"));
            String wlkmbs = Util.null2String(rs.getString("wlkmbs"));
            String ysbm = Util.null2String(rs.getString("ysbm"));
            String ysxm = Util.null2String(rs.getString("ysxm"));
            String ydzqysfyeyb = Util.null2String(rs.getString("ydzqysfyeyb"));
            String cwfhjebb = Util.null2String(rs.getString("cwfhjebb"));
            String yfpzhjnd = Util.null2String(rs.getString("yfpzhjnd"));
            String yfpzxxmbh = Util.null2String(rs.getString("yfpzxxmbh"));
            String zjmxxm = Util.null2String(rs.getString("zjmxxm"));


            String username = comInfo.getLastname(jbr);
            String requestname = "一般用款申请流程-" + username + "-" + curdate;
            String workflowid = getWorkflowid(formid);
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put("jbr", jbr);
            requestMainMap.put("jbrwb", comInfo.getLastname(jbr));
            requestMainMap.put("bm", bm);
            requestMainMap.put("gh", comInfo.getWorkcode(jbr));
            requestMainMap.put("sqrq", curdate);
            requestMainMap.put("yksxfl", "0");
            requestMainMap.put("qwzfrq", DateUtil.getLastDayOfMonth(curdate));
            requestMainMap.put("yksxsm", jyzfnr);
            requestMainMap.put("zffs", convertZflx(zffsxl));
            requestMainMap.put("hkfyqtxfksxbzdfyxxjljd", jyzfnr);
            requestMainMap.put("bcqkjehjxx", cwfhjeyb);
            requestMainMap.put("skdwxz", getGysId(ksbm));
            requestMainMap.put("gsdm", gsdm);
            requestMainMap.put("ssgs", ssgs);
            requestMainMap.put("ksbm", ksbm);
            requestMainMap.put("lxh", lxhdh);
            requestMainMap.put("khx", khyxdh);
            requestMainMap.put("zh", yxzhdh);
            requestMainMap.put("lxhcd", lxhcd);
            requestMainMap.put("khxcd", khyxcd);
            requestMainMap.put("yxzhcd", yxzhcd);
            requestMainMap.put("pldb", "0");
             requestid = WorkflowCreateByRequestMap(jbr, workflowid, requestname, "1", requestMainMap);
            if (Util.getIntValue(requestid, -1) > 0) {
                String tablename = "formtable_main_" + formid;
                String newmainid = getMainidByRequestid(requestid, tablename);
                Map<String, Object> map = new HashMap<>();
                map.put("zjjhxh", id);
                map.put("ly", "EC");
                map.put("zjmxxmwb", zjmxxmwb);
                map.put("zjjhxhwb", jhid);
                map.put("skdwdm", ksbm);
                map.put("skdw", ksmc);
                map.put("lzdjh", lzdjh);
                map.put("bz", bz);
                map.put("gsdm", gsdm);
                map.put("wlkmbh", wlkmbm);
                map.put("wlkm", wlkmbs);
                map.put("yydmxjlm", ysbm);
                map.put("xjlkm", ysxm);
                map.put("ydzqysfyeyb", ydzqysfyeyb);
                map.put("jeyb", cwfhjeyb);
                map.put("bcqkjeyb", cwfhjeyb);
                map.put("hl", "1");
                map.put("bcqkjebb", cwfhjebb);
                map.put("pldb", "0");
                map.put("yfpzhjnd", yfpzhjnd);
                map.put("yfpzxxmbh", yfpzxxmbh);
                map.put("zjmxxm", zjmxxm);
                map.put("jyzfnr", jyzfnr);
                map.put("mainid", newmainid);
                boolean flag = insertDetailData(tablename + "_dt1", map);
                if (flag) {
                    rs.executeUpdate("update uf_zjjhhzb set sfcffklc=0 where id=?", id);
                    succcount++;
                } else {
                    failcount++;
                }
            } else {
                failcount++;
            }
        }
        jsonData.put("counts", counts);
        jsonData.put("succcount", succcount);
        jsonData.put("failcount", failcount);
        out.print(jsonData.toJSONString());
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<%!
    BaseBean baseBean = new BaseBean();

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog("-----创建流程传递的参数个数--------");
        baseBean.writeLog("creatorId:" + creatorId);
        baseBean.writeLog("workflowId:" + workflowId);
        baseBean.writeLog("requestName:" + requestName);
        baseBean.writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建人Id
        requestInfo.setWorkflowid(workflowId);//工作流Id
        requestInfo.setDescription(requestName);//请求标题
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            baseBean.writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        baseBean.writeLog("-----创建流程传递的参数个数p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                baseBean.writeLog("=======" + e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            baseBean.writeLog(sbf.toString());
        } catch (Exception e) {
            baseBean.writeLog("错误：" + e);
        }
        return requestid;
    }

    /**
     *@Description: 获取最新版本号的workflowid
     *@Param: [formid]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/9/6
     */
    public static String getWorkflowid(String formid) {
        RecordSet rs = new RecordSet();
        String sql = "select nvl(activeversionid,id) from workflow_base where abs(formid)=? ";
        rs.executeQuery(sql, formid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getMainidByRequestid(String requestid, String tablename) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getGysId(String gysbm) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_sapgyszsj where trim(gysbm)=?", gysbm);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getGsxx(String gsdm) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_gsxx where trim(gsdm)=?", gsdm);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 插入明细表单信息
     */
    public boolean insertDetailData(String tablename, Map<String, Object> map) {
        String insertsql = "insert into " + tablename + "";
        String keyString = "";
        String valueString = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            keyString += key + ",";
            valueString += "'" + map.get(key) + "',";
        }

        if (keyString.endsWith(",")) {
            keyString = keyString.substring(0, keyString.length() - 1);
        }
        if (valueString.endsWith(",")) {
            valueString = valueString.substring(0, valueString.length() - 1);
        }
        insertsql += "(" + keyString + ") values(" + valueString + ")";
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(insertsql);
    }

    /**
     * 插入明细表单信息
     */
    public static String convertZflx(String zflx) {
        String newzffs = "";
        switch (zflx) {
            case "0":
                newzffs = "1";
                break;
            case "1":
                newzffs = "0";
                break;
            case "2":
                newzffs = "3";
                break;
            default:
                newzffs = "null";
        }
        return newzffs;
    }

    // 流程自动提交--requestid 请求id/operator 流程操作者/结果 boolean型
    public static boolean autoSubmit(String requestid,int operator){
        RequestService server = new RequestService();
        RequestInfo requestInfo = server.getRequest(Integer.parseInt(requestid));
        return new RequestService().nextNodeBySubmit(requestInfo, Integer.parseInt(requestid), operator, "");
    }
%>
